const mongoose = require('mongoose');
const { stringify } = require('querystring');
mongoose.connect('mongodb://localhost/runoob');

const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error'));
db.once('open', function () {
  console.log('db open');
});
// schema对象

// 创建一个和集合相关的schema对象 类似表头
// 获取schema对象
const userSchema = new mongoose.Schema({
  user: { type: String, require: true },
  password: { type: String, require: true },
  age: Number,
  sex: { type: Number, default: 0 },
});
// 将schema对象转换为数据模型
const User = mongoose.model('user', userSchema); // 该数据对象和集合关联('集合名',schema对象)
// 操作数据库
User.insertMany({ us: 'wangyi', ps: '123', age: 16 })
  .then((data) => {
    console.log(data);
    console.log('fine');
  })
  .catch((err) => {
    console.log('失败');
  });
